// 配合data.js
const mongoose = require("mongoose");

// 设置 strictQuery 为 true 去除提示
mongoose.set("strictQuery", true);

//3. 连接 mongodb 服务                        数据库的名称
mongoose.connect("mongodb://127.0.0.1:27017/bobo");

mongoose.connection.on("open", () => {
  let BookSchema = new mongoose.Schema({
    name: String,
    author: String,
    price: Number,
    is_hot: Boolean,
  });

  //6. 创建模型对象，对文档操作的封装对象
  // 参数一数据库文档名称 参数二类型限制
  let BookModel = mongoose.model("novel", BookSchema);

  //7.deleteOne删除一个
  BookModel.deleteOne({ name: "红楼梦" }, (err, data) => {
    //判断
    if (err) {
      console.log("删除失败~~~");
      return;
    }
    //输出 data
    console.log(data);
  });

  // 批量删除
  // BookModel.deleteMany({is_hot: false}, (err, data) => {});
});

// 设置连接错误的回调
mongoose.connection.on("error", () => {
  console.log("连接失败");
});

//设置连接关闭的回调
mongoose.connection.on("close", () => {
  console.log("连接关闭");
});
